//
//  SecurityTableViewController.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/21.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "SecurityTableViewController.h"
#import "HYNetHelper.h"
#import "ResetPasswordViewController.h"
#import "ResetPasswordNextViewController.h"
#import "ChangeVerifyView.h"
#import "GAAuthenticationViewController.h"

@interface SecurityTableViewController ()
AWPBool(isLink)
@end

@implementation SecurityTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = JFMainLocalizedString(@"myProfile.Safety");
    self.phoneSwitch.hidden = YES;
    
    self.resetPwdView.AWaddTapGestureRecognizer(self, @selector(resetPwdAction));
    self.faTitleLab.text =  JFMainLocalizedString(@"myProfile.tfa");
    self.smsTitleLab.text =  JFMainLocalizedString(@"myProfile.SMSAuthentication");
    self.gaTitleLab.text =  JFMainLocalizedString(@"myProfile.GAAuthentication");
    self.resetPwdTitleLab.text =  JFMainLocalizedString(@"myProfile.resetPwd");
    self.notlinkTitleLab.text =  JFMainLocalizedString(@"myProfile.notlinked");
 
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}
-(void)requestData{
    WEAKSELF
    [HYNetHelper allUsers:^(JFResultVo *resultVo) {
        BOOL googleflag = false;
        if (resultVo.errorNo == 0) {
            googleflag = [resultVo.results boolForKey:@"tfa_key_status"];
        }
        if (googleflag) {
             weakSelf.notlinkTitleLab.text =  JFMainLocalizedString(@"myProfile.linked");
        }else{
             weakSelf.notlinkTitleLab.text =  JFMainLocalizedString(@"myProfile.notlinked");
        }
        weakSelf.isLink = googleflag;
    }];
    [HYNetHelper users_2fa:^(JFResultVo *resultVo) {
        BOOL flag = false;
        if (resultVo.errorNo == 0) {
            flag = [resultVo.results boolForKey:@"enabled"];
        }
        weakSelf.googleSwitch.on = flag;
    }];
}
#pragma mark - Table view data source
-(void)resetPwdAction{
    ResetPasswordNextViewController *vc = [[UIStoryboard storyboardWithName:@"Login" bundle:nil]instantiateViewControllerWithIdentifier:@"ResetPasswordNextViewController"];;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)googleAuthClick:(UISwitch*)sender {
    [sender setOn:!sender.on animated:NO];
    if (self.isLink) {//绑定了的话 就是启用和关闭
        if (self.googleSwitch.on) {//关闭
            
        }else{//打开
            
        }
        
        ChangeVerifyView *view = [[[NSBundle mainBundle] loadNibNamed:@"ChangeVerifyView" owner:nil options:nil] firstObject];
        view.frame = CGRectMake(0, 0, CUSTOMVIEW_WIDTH, 110);
        FZAlertController *vc = [FZAlertController alertControllerWithTitleImage:[UIImage imageNamed:@"Captcha_ico_b"] title:nil customView:view message:nil];
        vc.showCloseBtn = YES;
        WEAKSELF
        FZAlertAction *action = [FZAlertAction actionWithTitle:JFMainLocalizedString(@"confirm") style:UIAlertActionStyleDefault handler:^(FZAlertAction *action) {
            NSString * verifyType  = view.verifyType;
            NSString * token  = view.codeText.text;
            [HYNetHelper users_2fa_toggle:!weakSelf.googleSwitch.on verifyType:verifyType token:token callback:^(JFResultVo *resultVo) {
                if (resultVo.errorNo == 0) {//成功就是开启
                    weakSelf.googleSwitch.on = !weakSelf.googleSwitch.on;
                }else{
                    [SLRemindView showMessage:@"成功" toView:weakSelf.view];
                }
            }];
        }];
        [vc addAction:action];
        [self presentViewController:vc animated:YES completion:nil];
    }else{//去绑定谷歌认证
        GAAuthenticationViewController *vc = [[UIStoryboard storyboardWithName:@"User" bundle:nil]instantiateViewControllerWithIdentifier:@"GAAuthenticationViewController"];;
        vc.title = self.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
@end
